package design_pattern.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class Demo {
    public static void main(String[] args) {
        RealImage realImage=new RealImage();
        Image proxyImage= (Image) Proxy.newProxyInstance(realImage.getClass().getClassLoader(), realImage.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                String m_name=method.getName();
                int second= (int) args[0];
                Object rtValue=null;
                if(m_name.equals("display")){
                    if(second>10){
                        System.out.println("下载当前图片");
                        rtValue=method.invoke(realImage,second/2);
                    }
                }
                return rtValue;
            }
        });
        proxyImage.display(20);

    }
}
